//package com.xiaocikuai.smallchunks.controller.Transaction;
//
//import com.xiaocikuai.smallchunks.entity.Transaction.*;
//import com.xiaocikuai.smallchunks.service.Transaction.RandomCode;
//import com.xiaocikuai.smallchunks.service.Transaction.TransactionServices;
//import com.xiaocikuai.smallchunks.service.Transaction.impl.MaleListServiceImpl;
//import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
//import jakarta.annotation.Resource;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.List;
//
////交易
//@RestController
//@RequestMapping("/api/transaction")
//@CrossOrigin
//public class TransactionController {
//
//    @Resource
//    private TransactionServices transactionService;
//
//    @Resource
//    private RandomCode randomCode;
//
//    @Resource
//    private MaleListServiceImpl maleListService;
//
//    //获取用户地址列表
//    @GetMapping("/getAddress")
//    public Resultd getAddress(@RequestHeader("Authorization") String token) {
//        List<Address> addresses =  transactionService.getAddress(token);
//            return Resultd.ok().data("info", addresses );
//
//    }
//
//    //添加地址
//    @PostMapping ("/addAddress")
//    public Resultd addAddress(@RequestHeader("Authorization") String token, @RequestBody Address address) {
//        if(transactionService.addAddress(token, address))
//            return Resultd.ok().data("info", "添加成功" );
//        else
//            return Resultd.error().data("token", "添加失败");
//    }
//
//    //修改地址
//    @PostMapping ("/updateAddress")
//    public Resultd updateAddress(@RequestHeader("Authorization") String token, @RequestBody Address address) {
//        if(transactionService.updateAddress(token, address))
//            return Resultd.ok().data("info", "更新成功" );
//        else
//            return Resultd.error().data("token", "更新失败");
//    }
//
//    //删除地址
//    @PostMapping ("/deleteAddress")
//    public Resultd deleteAddress(@RequestHeader("Authorization") String token, @RequestBody Address address) {
//        if(transactionService.deleteAddress(token, address))
//            return Resultd.ok().data("info", "删除成功" );
//        else
//            return Resultd.error().data("token", "删除失败");
//    }
//
//    @PostMapping ("/test")
//    public Resultd test(@RequestHeader("Authorization") String token, @RequestBody YshopOP yshopOP) {
//            return Resultd.ok().data("info", transactionService.addOrder(token, yshopOP));
//    }
//
//    //判断1小时之内是否有创建过的该商品订单 如果有 则直接返回1小时之内的订单
//    @PostMapping ("/addOrder")
//    public Resultd addOrder(@RequestHeader("Authorization") String token, @RequestBody  YshopOP yshopOP) {
//
//        if(yshopOP != null)
//            return Resultd.ok().data("info", transactionService.addOrder(token, yshopOP) );
//        else
//            return Resultd.error().data("info", "订单生成失败");
//    }
//
//
//
//    @PostMapping("orderlist")
//    public Resultd orderlist(@RequestHeader("Authorization") String token) {
//        return Resultd.ok().data("info", transactionService.ReturnOrderList()) ;
//    }
//
//    @PostMapping ("/updateYshopPay")
//    public Resultd updateYshopPay(@RequestHeader("Authorization") String token, @RequestBody Pay yshopPay) {
//            String result = transactionService.validatePromotionalInfo(token, yshopPay);
//            if (result.equals("验证通过") || result.equals("未使用优惠券")){
//                System.out.println(result);
//
//                YshopOP yshopOP = transactionService.updateYShopPay(token, yshopPay);
//                if(yshopOP != null)
//                    return Resultd.ok().data("info", yshopOP );
//                else
//                    return Resultd.error().data("info", "订单更新失败");
//            }else
//                return Resultd.error().data("info", result);
//    }
//
//    @PostMapping("/getOrderInfo")
//    public Resultd getOrderInfo(int oid, int payId) {
//        return Resultd.ok().data("info", transactionService.getOrderInfo(oid, payId)) ;
//    }
//
//
//    //获取该用户的优惠券
//    @PostMapping ("/getYshopPromotional")
//    public Resultd getYshopPromotional(@RequestHeader("Authorization") String token,@RequestBody YshopMaleList yshopMaleList) {
//        List<YshopPromotional> yshopPromotional =  transactionService.getYshopPromotional(token, yshopMaleList.getTid());
//        return Resultd.ok().data("info",  yshopPromotional);
//
//    }
//
//    @PostMapping ("/addOrderByCode")
//    public Resultd addOrderByCode(@RequestHeader("Authorization") String token, @RequestBody YshopOP yshopOP) {
////        if (transactionService.check(yshopOP.getOid())){
//            if (transactionService.addOrderByCode(token, yshopOP)){
//                return Resultd.ok().data("info","兑换成功" );
//            }else
//                return Resultd.error().data("info", "兑换失败");
////        }else
////            return Resultd.error().data("info", "该订单已支付");
//    }
//
//
//
//    @PostMapping ("/orderUserListReturn")
//    public Resultd orderUserListReturn(@RequestHeader("Authorization") String token) {
//        List<ReturnOrderList> returnOrderLists = transactionService.orderUserListReturn(token);
//        if(returnOrderLists != null){
//            return Resultd.ok().data("info", returnOrderLists );
//        }else{
//            return Resultd.error().data("info", "错误");
//        }
//
//    }
//
//    //获取商品表
//    @PostMapping ("/getMaleLists")
//    public Resultd getMaleLists(@RequestHeader("Authorization") String token, @RequestBody  MaleList maleList) {
//        List<MaleList> maleLists = maleListService.getMaleLists(maleList.getTag());
//        if(maleLists != null){
//            return Resultd.ok().data("info", maleLists );
//        }else{
//            return Resultd.error().data("info", "tag错误");
//        }
//
//    }
//
//
//    //验证是否支付
//    @PostMapping ("/getIsPay")
//    public Resultd getIsPay(@RequestHeader("Authorization") String token, @RequestBody YshopOrder yshopOrder) {
//        if(transactionService.check(yshopOrder.getOid()) != null){
//            return Resultd.ok().data("info", "支付成功");
//        }else{
//            return Resultd.error().data("info", "支付失败");
//        }
//
//    }
//
//
//
//
//
//
//
//
//
//}
package com.xiaocikuai.smallchunks.controller.Transaction;

import com.xiaocikuai.smallchunks.entity.Transaction.*;
import com.xiaocikuai.smallchunks.service.Transaction.TransactionServices;
import com.xiaocikuai.smallchunks.service.Transaction.impl.MaleListServiceImpl;
import com.xiaocikuai.smallchunks.service.Transaction.impl.OrderServiceImpl;
import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

// 交易
@Api(tags = "交易控制器")
@RestController
@RequestMapping("/api/transaction")
@CrossOrigin
public class TransactionController {

    @Resource
    private TransactionServices transactionService;
    @Resource
    private MaleListServiceImpl maleListService;
    @Resource
    private OrderServiceImpl orderService;

    @ApiOperation(value = "获取用户地址列表")
    @PostMapping("/getAddress")
    @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header")
    public Resultd getAddress(@RequestHeader("Authorization") String token) {
        List<Address> addresses = transactionService.getAddress(token);
        return Resultd.ok().data("info", addresses);
    }

    @PostMapping("/getAddressInfo")
    @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header")
    public Resultd getAddressInfo(@RequestHeader("Authorization") String token,@RequestBody Address address) {
        Address addresses = transactionService.getAddressInfo(address.getAid());
        if (addresses != null)
            return Resultd.ok().data("info", addresses);
        else
            return Resultd.error().data("info","发生错误，请重试");
    }

    @ApiOperation(value = "添加地址")
    @PostMapping("/addAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "address", value = "地址信息", required = true, dataType = "Address", paramType = "body")
    })
    public Resultd addAddress(@RequestHeader("Authorization") String token, @RequestBody Address address) {
        if (transactionService.addAddress(token, address))
            return Resultd.ok().data("info", "添加成功");
        else
            return Resultd.error().data("token", "添加失败");
    }

    @ApiOperation(value = "修改地址")
    @PostMapping("/updateAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "address", value = "地址信息", required = true, dataType = "Address", paramType = "body")
    })
    public Resultd updateAddress(@RequestHeader("Authorization") String token, @RequestBody Address address) {
        if (transactionService.updateAddress(token, address))
            return Resultd.ok().data("info", "更新成功");
        else
            return Resultd.error().data("token", "更新失败");
    }

    @ApiOperation(value = "删除地址")
    @PostMapping("/deleteAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "address", value = "地址信息", required = true, dataType = "Address", paramType = "body")
    })
    public Resultd deleteAddress(@RequestHeader("Authorization") String token, @RequestBody Address address) {
        if (transactionService.deleteAddress(token, address))
            return Resultd.ok().data("info", "删除成功");
        else
            return Resultd.error().data("token", "删除失败");
    }



    @ApiOperation(value = "测试方法")
    @PostMapping("/test")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "yshopOP", value = "交易操作信息", required = true, dataType = "YshopOP", paramType = "body")
    })
    public Resultd test(@RequestHeader("Authorization") String token, @RequestBody YshopOP yshopOP) {
        return Resultd.ok().data("info", transactionService.addOrder(token, yshopOP));
    }

    @ApiOperation(value = "添加订单（判断一小时内是否有相同商品订单）")
    @PostMapping("/addOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "yshopOP", value = "交易操作信息", required = true, dataType = "YshopOP", paramType = "body")
    })
    public Resultd addOrder(@RequestHeader("Authorization") String token, @RequestBody YshopOP yshopOP) {
        String result = transactionService.checkProductE(yshopOP.getTid(), token);
        if (result.equals("success")){
            return Resultd.ok().data("info", transactionService.addOrder(token, yshopOP));
        }
            return Resultd.error().data("info", result);
    }

    @ApiOperation(value = "获取我的订单列表")
    @PostMapping("/orderlist")
    @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header")
    public Resultd orderlist(@RequestHeader("Authorization") String token) {
        return Resultd.ok().data("info", transactionService.ReturnOrderList(token));
    }

    @ApiOperation(value = "我的订单")
    @PostMapping("/myOrder")
    public Resultd selectMyOrder(@RequestHeader("Authorization") String token,@RequestBody Order order){
        List<OrderVO> orders = orderService.selectUserOrder(order, token);
        if (orders!= null)
            return Resultd.ok().data("info", orders);
        else
            return Resultd.error().data("info", "订单生成失败");
    }


    @ApiOperation(value = "更新订单支付信息")
    @PostMapping("/updateYshopPay")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "yshopPay", value = "支付信息", required = true, dataType = "Pay", paramType = "body")
    })
    public Resultd updateYshopPay(@RequestHeader("Authorization") String token, @RequestBody Pay yshopPay) throws IOException {
        String result = transactionService.validatePromotionalInfo(token, yshopPay);
        if (result.equals("验证通过") || result.equals("未使用优惠券")) {
            System.out.println(result);

            YshopOP yshopOP = transactionService.updateYShopPay(token, yshopPay);
            if (yshopOP!= null)
                return Resultd.ok().data("info", yshopOP);
            else
                return Resultd.error().data("info", "订单更新失败");
        } else
            return Resultd.error().data("info", result);
    }

    @ApiOperation(value = "获取订单信息")
    @PostMapping("/getOrderInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oid", value = "订单 ID", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "payId", value = "支付 ID", required = true, dataType = "int", paramType = "query")
    })
    public Resultd getOrderInfo(@RequestBody Order order) {
        return Resultd.ok().data("info", transactionService.getOrderInfo(order.getOid()));
    }

    @ApiOperation(value = "获取用户的优惠券")
    @PostMapping("/getYshopPromotional")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "yshopMaleList", value = "用户信息", required = true, dataType = "YshopMaleList", paramType = "body")
    })
    public Resultd getYshopPromotional(@RequestHeader("Authorization") String token, @RequestBody YshopMaleList yshopMaleList) {
        List<YshopPromotional> yshopPromotional = transactionService.getYshopPromotional(token, yshopMaleList.getTid());
        if (yshopPromotional != null)
            return Resultd.ok().data("info", yshopPromotional);
        else
            return Resultd.error().data("info","您还没有优惠券，快去领取吧！");
    }



    @ApiOperation(value = "通过兑换码添加订单")
    @PostMapping("/addOrderByCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "yshopOP", value = "交易操作信息", required = true, dataType = "YshopOP", paramType = "body")
    })
    public Resultd addOrderByCode(@RequestHeader("Authorization") String token, @RequestBody YshopOP yshopOP) throws IOException{
        //        if (transactionService.check(yshopOP.getOid())){
        if (transactionService.addOrderByCode(token, yshopOP)) {
            return Resultd.ok().data("info", "兑换成功");
        } else
            return Resultd.error().data("info", "兑换失败");
        //        }else
        //            return Resultd.error().data("info", "该订单已支付");
    }

    @ApiOperation(value = "获取用户的退货订单列表")
    @PostMapping("/orderUserListReturn")
    @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header")
    public Resultd orderUserListReturn(@RequestHeader("Authorization") String token) {
        List<ReturnOrderList> returnOrderLists = transactionService.orderUserListReturn(token);
        if (returnOrderLists!= null) {
            return Resultd.ok().data("info", returnOrderLists);
        } else {
            return Resultd.error().data("info", "错误");
        }
    }

    @ApiOperation(value = "获取商品表")
    @PostMapping("/getMaleLists")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "maleList", value = "商品列表筛选信息", required = true, dataType = "MaleList", paramType = "body")
    })
    public Resultd getMaleLists(@RequestHeader("Authorization") String token, @RequestBody MaleList maleList) {
        List<MaleList> maleLists = maleListService.getMaleLists(maleList.getTag());
        if (maleLists!= null) {
            return Resultd.ok().data("info", maleLists);
        } else {
            return Resultd.error().data("info", "tag 错误");
        }
    }

    @ApiOperation(value = "验证订单是否支付")
    @PostMapping("/getIsPay")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "yshopOrder", value = "订单信息", required = true, dataType = "YshopOrder", paramType = "body")
    })
    public Resultd getIsPay(@RequestHeader("Authorization") String token, @RequestBody YshopOrder yshopOrder) {

            return Resultd.ok().data("info", transactionService.check(yshopOrder.getOid()));

    }




}